<template>
  <div id="app">
    <el-card shadow="always">
      <el-form :inline="true" :model="studentsearch" class="demo-form-inline">
        <el-form-item >
          <el-input v-model="studentsearch.sname" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item >
          <el-input v-model="studentsearch.addr" placeholder="住址"></el-input>
        </el-form-item>
        <el-form-item >
          <el-select v-model="studentsearch.cid" placeholder="所在班级">
            <el-option label="所有班级" value="0"></el-option>
            <el-option :label="c.cname" value="c.cid" v-for="c in classeslist" :key="c.cid"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" plain @click="search">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table
          :data="studentlist"
          style="width: 100%">
        <el-table-column
            label="学号"
            prop="sid"
            align="centry"
            width="180">
        </el-table-column>
        <el-table-column
            label="姓名"
            prop="sname"
           align="centry">
        </el-table-column>
        <el-table-column
            label="性别"
            prop="sex"
            align="centry">
        </el-table-column>
        <el-table-column
            label="年龄"
            prop="age"
            align="centry">
        </el-table-column>
        <el-table-column
            label="家庭住址"
            prop="addr"
            align="centry">
        </el-table-column>
        <el-table-column
            label="班级号"
            prop="cid"
            align="centry">
        </el-table-column>
        <el-table-column
            label="班级名"
            prop="cname"
            align="centry">
        </el-table-column>
        <el-table-column label="操作" >
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
<!--      分页-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import studentApi from "@/api/student"
import classesApi from "@/api/classes"
export default {
  data() {
    return {
      studentlist: [],
      page: 1,                 //当前页码
      pagesize: 5,             //每页显示条数
      total: 100,
      studentsearch: {},
      classeslist:[],
    }
  },
  created() {
    // this.findallstudents()
    // this.findbypage(this.page,this.pagesize)//分页
    this.findallclasses()//所有班级列表列出
    this.search()
  },
  methods: {
   async  findallstudents(){
     const {data:a}=await studentApi.findallstudents()
     console.log(data.a)
     if(a.code===200){
       this.studentlist=a.data.list
     }
   },
    async findbypage(){
      const {data:a}=await studentApi.findbypage(this.page,this.pagesize)
      if(a.code===200){
        this.studentlist=a.data.aa.rows
        this.total=a.data.aa.total
      }
    },
    async findallclasses(){
      const {data:a}=await classesApi.findallclasses()
      if(a.code===200){
        this.classeslist=a.data.list
      }
    },
    async search(){
      const {data:a}=await studentApi.search(this.page,this.pagesize,this.studentsearch)
      if(a.code===200){
        this.studentlist=a.data.aa.rows
        this.total=a.data.aa.total
      }
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    handleSizeChange(val) {
      this.pagesize=val;
      // this.findbypage();
      this.search()
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
     this.page=val;
     // this.findbypage();
      this.search()
      console.log(`当前页: ${val}`);
    }
  }
}
</script>

<style>
#app{
  display: flex;
  justify-content: center;
}

.el-card{
  width: 90%;
}
.el-pagination{
  margin-top: 20px;
  text-align: right;
}
</style>
